package otl;


//import java.util.List;

import processing.core.*;
import processing.data.*;
import processing.core.PFont;
import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.geo.Location;
import de.fhpotsdam.unfolding.marker.SimplePointMarker;
import de.fhpotsdam.unfolding.providers.StamenMapProvider;
import de.fhpotsdam.unfolding.utils.MapUtils;
import de.fhpotsdam.unfolding.utils.ScreenPosition;
import otl.overlay.*;

@SuppressWarnings("serial")
public class OnTheLine extends PApplet {

		//
		int appWidth = 1600;
		int appHeight = 1000;
	
		//REGION OUTER BOUNDARIES -- NEEDED FOR GEO-PIXEL MAPPINGS 
		static float mapGeoLeft   = (float) -80.6678;          // Longitude 
		static float mapGeoRight  =  (float) -80.1816;          // Longitude 
		static float mapGeoTop    =   (float) 43.559;          // Latitude 
		static float mapGeoBottom =  (float) 43.275;          // Latitude 
		                         
		
		//GLOBAL STYLES
		PFont font = createFont("serif-bold", 12);
		PFont H1_font = createFont("serif-bold", 12);
		
		
		//map stuff - starting coords
		static int mapMaxZoomLevel   = 12;
		static int mapMinZoomLevel   = 19;
		Location kcw = new Location(43.42f, -80.35f); // kcw (latitude, longitude) 
		float maxPanningDistance = 15;
		
		static float mapScreenWidth,mapScreenHeight;  // Dimension of map in pixels.
		
		

		//GLOBAL FILE URLS
		String bgImageURL = "../GraphicAssets/region01.png";
		String stationsCSV_URL = "../DataAssets/stationLocations.csv";
		String placesCSV_URL = "../DataAssets/places.csv";
		
		
		
		
		UnfoldingMap map;
		MapData mapData;
		
		//GRAPHICS
		Overlay overlay;
				
		
		public void setup() {
			size(appWidth,appHeight,P2D);
			//smooth();
			
			//mapScreenWidth  = width;
			//mapScreenHeight = height;
			
			map = new UnfoldingMap(this,new StamenMapProvider.Toner());
			map.zoomAndPanTo(kcw, 12); // set map starting location and zoom level
			map.setPanningRestriction(kcw, maxPanningDistance); 
			map.setZoomRange(mapMaxZoomLevel, mapMinZoomLevel);
			MapUtils.createDefaultEventDispatcher(this, map);
			
			
			mapData = new MapData(this,map);
			Table stationtable = loadTable(stationsCSV_URL,"header");
			Table placestable = loadTable(placesCSV_URL, "header");
			mapData.mapInit(stationtable,placestable);
			
			
			PFont font = createFont("serif-bold", 12);
			textFont(font);
			
			//Static Graphics set up
			overlay = new Overlay(this);
		}
		
		public void draw() {
			//image(backgroundMap,0,0,mapScreenWidth,mapScreenHeight);
			map.draw();
			fill(215, 0, 0, 100);
			// Shows latitude,longitude at mouse position
			Location location = map.getLocation(mouseX, mouseY);
			text("geo:" + location.toString(), mouseX, mouseY);
			
			mapData.drawMapData();
			
			overlay.drawMask();
		}		

		//include to run as java application instead of applet
		public static void main(String args[]) {
			PApplet.main(new String[] { "--present", "otl.OnTheLine" });
		}
		
		
		////////////////PROTECTED METHODS--------------------------------------------------
		
		// Converts screen coordinates into geographical coordinates. 
		// Useful for interpreting mouse position.
		protected static PVector pixelToGeo(PVector screenLocation)
			{
			    return new PVector(mapGeoLeft + (mapGeoRight-mapGeoLeft)*(screenLocation.x)/mapScreenWidth,
			                       mapGeoTop - (mapGeoTop-mapGeoBottom)*(screenLocation.y)/mapScreenHeight);
			}

		// Converts geographical coordinates into screen coordinates.
		// Useful for drawing geographically referenced items on screen.
		protected static PVector geoToPixel(PVector geoLocation)
			{
			    return new PVector(mapScreenWidth*(geoLocation.x-mapGeoLeft)/(mapGeoRight-mapGeoLeft),
			                       mapScreenHeight - mapScreenHeight*(geoLocation.y-mapGeoBottom)/(mapGeoTop-mapGeoBottom));
			}
		
		

}
